package com.maxie.proxy;

import com.maxie.proxy.annotion.ParamLog;
import com.maxie.proxy.service.PersonService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 打印入参、出参
 */
public class PersonInvocationHandle implements InvocationHandler {

    private PersonService personService;

    public PersonInvocationHandle(PersonService personService) {
        this.personService = personService;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ParamLog annotation = method.getAnnotation(ParamLog.class);
        String currentMethodName = method.getName();
        if (annotation != null) {
            //
            System.out.println("方法："+ currentMethodName +"入参：" + args[0].toString());
            // 执行方法
            Object invoke = method.invoke(personService, args);
            System.out.println("方法："+ currentMethodName +"出参：" + invoke.toString());
            return invoke;
        } else {
            return method.invoke(personService, args);

        }
    }
}
